package com.app.shop.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.controller.system.BaseController;
import com.app.shop.entity.WgCommodity;
import com.app.shop.entity.query.WgCommodityQuery;
import com.app.shop.entity.show.BaseShow;
import com.app.shop.entity.show.WgCommodityShow;
import com.app.shop.service.shop.WgCommodityService;
import com.app.shop.util.ApiRest;
import com.app.shop.util.ClassUtil;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 商品服务类Controller
 *
 * @author  chen li wei
 * @version 21:14 2019-5-10
 */
@Controller
@RequestMapping(value = "/wgCommodity")
public class WgCommodityController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WgCommodityController.class);
    @Autowired
    WgCommodityService wgCommodityService;

    /**
     * 查询商品列表
     * @param wgCommodityQuery 查询商品参数
     * @return 返回商品列表
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getCommodityList")
    public ApiRest getDictList(@RequestBody WgCommodityQuery wgCommodityQuery) {
        log.debug(JSONObject.toJSON(wgCommodityQuery).toString());
        //查询
        Page<WgCommodity> wgCommodityList=wgCommodityService.getWgCommodityList(wgCommodityQuery);
        //查询结果对象置换
        List<WgCommodityShow> resList=new ArrayList<WgCommodityShow>();
        WgCommodityShow wgCommodityShow;
        //数据转换
        for(WgCommodity wgCommodity:wgCommodityList){
            wgCommodityShow=new WgCommodityShow();
            try {
                ClassUtil.fatherToChild(wgCommodity,wgCommodityShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            wgCommodityShow.dataSet();
            resList.add(wgCommodityShow);
        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,
                new BaseShow(wgCommodityList.getPageNum(), wgCommodityList.getPageSize(), wgCommodityList.getTotal(), wgCommodityList.getPages(), resList));    }

    /**
     * 获取商品信息
      * @param wgCommodity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCommodityById")
    public ApiRest getCommodityById(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity  = wgCommodityService.getCommodityById(wgCommodity);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, wgCommodity);
    }

    /**
     * 商品上架
     * @param wgCommodity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editCommodityUp")
    public ApiRest editCommodityUp(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgCommodity.setModifiedDate(new Date());
        SysModel sysModel = wgCommodityService.editCommodityUp(wgCommodity);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 商品下架
     * @param wgCommodity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editCommodityDown")
    public ApiRest editCommodityDown(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgCommodity.setModifiedDate(new Date());
        SysModel sysModel = wgCommodityService.editCommodityDown(wgCommodity);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 商品删除
     * @param wgCommodity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editCommodityDelete")
    public ApiRest editCommodityDelete(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgCommodity.setModifiedDate(new Date());
        SysModel sysModel = wgCommodityService.editCommodityDelete(wgCommodity);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }


    /**
     * 添加商品
     * @param wgCommodity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addCommodity")
    public ApiRest addCommodity(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity.setCreateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        wgCommodity.setCreateDate(new Date());
        SysModel sysModel = wgCommodityService.addCommodity(wgCommodity);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 商品信息修改
     *
     * @param wgCommodity 需要修改的商品信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "editCommodity")
    public ApiRest editCommodity(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = wgCommodityService.editCommodity(wgCommodity);
        wgCommodity.setModifiedDate(new Date());
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 商品图文信息修改
     *
     * @param wgCommodity 需要修改的商品信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "editCommodityArticle")
    public ApiRest editCommodityArticle(@RequestBody WgCommodity wgCommodity) {
        log.debug(JSONObject.toJSON(wgCommodity).toString());
        wgCommodity.setModifiedUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = wgCommodityService.editCommodityArticle(wgCommodity);
        wgCommodity.setModifiedDate(new Date());
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }





}
